package com.tpfinal.agenda.controller;

import java.util.ArrayList;
import java.util.HashMap;

import com.asynccommand.framework.CommandsController;
import com.asynccommand.framework.ICommandListener;
import com.login.framework.ILoginListener;
import com.login.framework.LoginFramework;
import com.tpfinal.agenda.core.AgendaActionParamsCodes;
import com.tpfinal.agenda.core.AgendaConfig;
import com.tpfinal.agenda.core.AgendaRequestFactory;
import com.tpfinal.agenda.core.mysqlrequests.MySqlRequestFactory;
import com.tpfinal.agenda.model.City;
import com.tpfinal.agenda.model.Contact;

public class MainController {

	private static MainController instance;

	private LoginFramework loginFramework;

	private AgendaRequestFactory requestFactory;

	private ArrayList<Contact> contactsList;

	private Contact selectedContact;

	private ArrayList<City> citiesList;

	private MainController() {
		loginFramework = LoginFramework.get();
		loginFramework.init(AgendaConfig.get());
		requestFactory = new MySqlRequestFactory();
	}

	public static MainController get() {
		if (instance == null) {
			instance = new MainController();
		}
		return instance;
	}

	public void register(ILoginListener listener) {
		loginFramework.register(listener);
	}

	public void register(ICommandListener listener) {
		CommandsController.get().register(listener);
	}

	public void unregister(ILoginListener listener) {
		loginFramework.unregister(listener);
	}

	public void unregister(ICommandListener listener) {
		CommandsController.get().unregister(listener);
	}

	/**
	 * Eecuta el proceso de login haciendo uso del framework.
	 * 
	 * @param username
	 * @param password
	 */
	public void login(String username, String password) {
		loginFramework.performLogin(username, password);
	}

	/**
	 * 
	 * Carga desde el repositorio de datos la lista de contactos.
	 * 
	 */
	public void loadContactsList() {
		requestFactory.createGetAllContactsRequest().execute();
	}

	/**
	 * Carga desde el repositorio de datos la lista de localidades
	 * 
	 */
	public void loadCitiesList() {
		requestFactory.createGetAllCitiesRequest().execute();
	}

	/**
	 * Agrega un contacto.
	 * 
	 * @param c
	 */
	public void addContact(Contact c) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put(AgendaActionParamsCodes.PARAM_CONTACT, c);
		requestFactory.createInsertContactRequest().execute(params);
	}

	/**
	 * Elimina un contacto
	 * 
	 * @param c
	 */
	public void deleteContact(Contact c) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put(AgendaActionParamsCodes.PARAM_CONTACT, c);
		requestFactory.createDeleteContactRequest().execute(params);
		requestFactory.createGetAllContactsRequest().execute();
	}

	/**
	 * Modifica un contacto
	 * 
	 * @param c
	 */
	public void updateContact(Contact c) {
		HashMap<String, Object> params = new HashMap<String, Object>();
		params.put(AgendaActionParamsCodes.PARAM_CONTACT, c);
		requestFactory.createUpdateContactRequest().execute(params);
		requestFactory.createGetAllContactsRequest().execute();
	}

	/**
	 * Devuelve la lista cargada en memoria de contactos.
	 * 
	 * @return
	 */
	public ArrayList<Contact> getContactsList() {
		return contactsList;
	}

	public void setContactsList(ArrayList<Contact> contactsList) {
		this.contactsList = contactsList;
	}

	public Contact getSelectedContact() {
		return selectedContact;
	}

	public void setSelectedContact(Contact selectedContact) {
		this.selectedContact = selectedContact;
	}

	public void setSelectedContact(int id) {
		Contact c = getContactById(id);
		setSelectedContact(c);
	}

	public Contact getContactById(int id) {

		for (Contact contact : this.contactsList) {
			if (contact.getId() == id) {
				return contact;
			}
		}

		return null;

	}

	public void setCitiesList(ArrayList<City> citiesList) {
		this.citiesList = citiesList;
	}

	public ArrayList<City> getCitiesList() {
		return citiesList;
	}

}
